<?php
//require section
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_session.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_inside_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_tickets.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_combos.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_redirect.php';
$title = 'Coupon Code & Finish';

$payment = filter_input(INPUT_GET, "payment");


$cb_enable_combo = filter_input(INPUT_POST, "cb_enable_combo");
$combo_quantity = filter_input(INPUT_POST, "txt_combo_quantity");
$combo_id = filter_input(INPUT_POST, "ddl_combo");
$_SESSION['book_info']['combo_quantity'] = 0;
$_SESSION['book_info']['combo_id'] = 0;
if (!empty($combo_id) && !empty($combo_quantity)) {
    $_SESSION['book_info']['combo_quantity'] = $combo_quantity;
    $_SESSION['book_info']['combo_id'] = $combo_id;
}
?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
        ?>
        <script type="text/javascript">
            
            function add_coupon_code() {
                var coupon_code = $('input[name="txt_coupon_code"]').val();
                $.ajax({
                    type: "POST",
                    url: "<?php echo LINK_ROOT . DIR_AJAX; ?>accept_coupon_code.php",
                    data: "coupon_code="+coupon_code,
                    success: function (result) {
                        var data_json = jQuery.parseJSON( result );
                        var info_com = $("#coupon_code_info");
                        if (data_json.successful) {
                            $("#td_total").html(data_json.total);
                            $('input[name="txt_coupon_code"]').attr('disabled', 'disabled');
                            $("#btn_add_coupon_code").hide();
                            $("#td_coupon_code").removeClass();
                            $("#td_coupon_code").addClass("ui three column row");
                            info_com.removeClass();
                            info_com.addClass("ui green label");
                        } else {
                            info_com.removeClass();
                            info_com.addClass("ui orange label");
                        }
                        info_com.html(data_json.info);
                    }
                });
            }
            
            function checkout() {
                $('#checkout').modal('show');
            }
            
            function finalize_booking(payment_method) {
                $.ajax({
                    type: "POST",
                    data: "payment_method="+payment_method,
                    url: "<?php echo LINK_ROOT . DIR_AJAX; ?>finalize_booking.php",
                    success: function (result) {
                        if (payment_method === "skrill") {
                            window.location.href = "<?php echo LINK_ROOT . DIR_SKRILL_PAYMENT; ?>skrill.php";
                        }
                    }
                });
            }
            
            function easypaisa() {
                finalize_booking("easypaisa");
                $('#easypaisa').modal('setting', {
                    closable  : false,
                    onHidden : function() {
                        window.location.href = "<?php echo LINK_ROOT; ?>/index.php";
                    }
                  }).modal('show');
                $('#btn_cancel').click();
            }
            
            function skrill() {
                finalize_booking("skrill");
//                $('#btn_cancel').click();
            }
            
            
        </script>
    </head>
    <body>
        <div class="ts-body-container">
            <div class="ts-wrapper">
                <div class="padding-top-large row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php';
                    ?>
                </div>
                <div class="ui grid">
                    <div class="row">
                        <div class="column">
                            <!-- Step -->
                            <div id='booking_step' class="ui four steps margin-top-normal">
                                <div class="ui disabled step">
                                    Choose Cinema & Time Start
                                </div>
                                <div class="ui disabled step">
                                    Choose Seats
                                </div>
                                <div class="ui disabled step">
                                    Choose Combo
                                </div>
                                <div class="ui active step">
                                    Coupon Code & Finish
                                </div>
                            </div>
                            <!-- End Step -->

                            <h1>Coupon Code & Finish</h1>

                            <?php
                            $ticket = api_tickets::get_ticket_by_id($_SESSION['book_info']['ticket_id']);
                            if (!empty($_SESSION['book_info']['combo_id'])) {
                                $combo = api_combos::get_combo_by_id($_SESSION['book_info']['combo_id']);
                            }
                            ?>
                            <form id="form_finish" action="" method="post" name="form_finish">
                                    <table class="ui orange table segment">
                                        <tr>
                                            <th>Movie:</th>
                                            <td><?php echo $ticket['movie_title']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Cinema:</th>
                                            <td><?php echo $ticket['cinema_name']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Date:</th>
                                            <?php
                                                $date = date_create($ticket["show_date"]);
                                                $date_str = date_format($date, "d/m/Y");
                                            ?>
                                            <td><?php echo $date_str; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Time:</th>
                                            <?php
                                                $date = date_create($ticket["time_start"]);
                                                $date_str = date_format($date, "H:i");
                                            ?>
                                            <td><?php echo $date_str; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Ticket Quantity:</th>
                                            <td><?php echo $_SESSION['book_info']['ticket_quantity']; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Seat:</th>
                                            <?php
                                            $list = api_booking::get_all_booking_lines_by_booking_id(
                                                    $_SESSION['book_info']['booking_id'],
                                                    $_SESSION['book_info']['user_id']);
                                            ?>
                                            <td>
                                                <?php
                                                if (empty($list) || count($list) == 0) {
                                                    echo "-";
                                                } else {
                                                    echo $list[0]['row_pos'] . $list[0]['column_pos'];
                                                    for ($index = 1; $index < count($list); $index++) {
                                                        echo ", " . $list[$index]['row_pos'] . $list[$index]['column_pos'];
                                                    }
                                                }
                                                ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th>Combo:</th>
                                            <?php
                                            $text = "-";
                                            if (!empty($combo['name'])) {
                                                $text = $combo['name'];
                                            }
                                            ?>
                                            <td><?php echo $text; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Combo Quantity:</th>
                                            <?php
                                            $text = "-";
                                            if (!empty($_SESSION['book_info']['combo_quantity'])) {
                                                $text = $_SESSION['book_info']['combo_quantity'];
                                            }
                                            ?>
                                            <td><?php echo $text; ?></td>
                                        </tr>
                                        <tr>
                                            <th>Total:</th>
                                            <?php
                                            $total = api_booking::calculate_total_money(
                                                    $ticket['id'], 
                                                    $_SESSION['book_info']['ticket_quantity'], 
                                                    $_SESSION['book_info']['combo_id'], 
                                                    $_SESSION['book_info']['combo_quantity'], 
                                                    $_SESSION['book_info']['combo_id']);
                                            ?>
                                            <td id="td_total" style="font-weight: bold;">$<?php echo $total; ?></td>
                                        </tr>
                                        <tr>
                                            <?php
                                            if (!empty($_SESSION['book_info']['coupon_id'])) {
                                                $coupon_id = $_SESSION['book_info']['coupon_id'];
                                            }
                                            $coupon_code = "";
                                            if (!empty($coupon_id)) {
                                                $coupon_code = $_SESSION['book_info']['coupon_code'];
                                            }
                                            ?>
                                            <th>Coupon Code:</th>
                                            <?php 
                                            if (!empty($coupon_id)) {
                                            ?>
                                            <td class="ui three column row">
                                                
                                                
                                                
                                                <input name="txt_coupon_code" type="text" value="<?php echo $coupon_code; ?>" placeholder="A1123D" disabled="disabled"/>
                                                <div id="btn_add_coupon_code" onclick="add_coupon_code();" class="ui orange button" style="display: none;">Add</div>
                                                <div id="coupon_code_info" class="ui green label">Coupon code accepted.</div>
                                            </td>
                                                <?php
                                                } else {
                                                ?>
                                            <td id="td_coupon_code" class="ui action input">
                                                <input name="txt_coupon_code" type="text" placeholder="A1123D" />
                                                <div id="btn_add_coupon_code" onclick="add_coupon_code();" class="ui orange button">Add</div>
                                                <div id="coupon_code_info"></div>
                                            </td>
                                                <?php
                                                }
                                                ?>
                                            
                                        </tr>
                                    </table>
                                <div class="ui orange segment">
                                    <div class="ts-center">
                                        <div class="ui buttons">
                                            <!--<div class="ui green button">Back</div>-->
                                            <a href="choose_combo.php#booking_step" class="ui purple button">Back</a>
                                            <div class="or"></div>
                                            <!--<div class="ui orange button">Next</div>-->
                                            <div onclick="checkout();" class="ui orange button">Checkout</div>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>

<!-- BEGIN CHECKOUT MODAL -->
<div id="checkout" class="ui modal">
  <i class="close icon"></i>
  <div class="header">
    Checkout
  </div>
  <div class="content">
    <div class="left ts-center">
        <div class="field">
        <img width="100%"  src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS; ?>skrill.png" />
        </div>
        <div class="ui buttons">
            <div onclick="skrill();" class="ui orange button">Pay via SKRILL</div>
        </div>
    </div>
    <div class="right ts-center">
        <div class="field">
        <img width="100%" src="<?php echo LINK_ROOT . DIR_SHARED_LAYOUT_IMAGES_ICONS; ?>easypaisa.png" />
        </div>
        <div class="ui buttons">
            <div onclick="easypaisa();" class="ui green button">Private Transfer</div>
        </div>
    </div>
  </div>
  <div class="actions">
    <div id="btn_cancel" class="ui button">
      Cancel
    </div>
  </div>
</div>
<!-- END OF CHECKOUT MODAL -->  

<!-- EASYPAISA MODAL -->
<div id="easypaisa" class="ui modal">
  <i class="close icon"></i>
  <div class="header">
    EASYPAISA Finish
  </div>
  <div class="content">
      <p>Congratulations, Booking successfully! Now, a booking code is sent to your email and phone number. </p>
      <p>You have to use the code to active your booking via easypaisa payment. (please fill the booking code in your payment.)</p>
      <br/>
      <p><strong>Please pay for our Easypaisa's account</strong></p>
      <p><?php echo api_config::get_easypaisa_detail(); ?></p>
  </div>
  <div class="actions">
    <div class="ui positive button">
      OK
    </div>
  </div>
</div>
<!-- END EASYPAISA MODAL -->

                <div class="row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
                    ?>
                </div>
            </div>
        </div>
    </body>
</html>